<?php

/**
 * set top parent id and sub parent id session. it`s use for menu check.
 *
 * @param  array $data menu info
 * @param  array $currentData current menu info
 * @return void
 */
function setCurrentPidSidSession($data, $currentData)
{
    if($currentData['level'] == '1' && $currentData['pid'] == '0')
    {
        return Scstore::setCurrentMenuPid($currentData['id']);
    }
    else
    {
        if($currentData['level'] == '2') Scstore::setCurrentMenuSid($currentData['id']);
        $parentData = getDataByKey($data, $currentData['pid']);
        setCurrentPidSidSession($data, $parentData);
    }
    return true;
}

/**
 * get data by array key
 * 
 * @param  array $data
 * @param  mixed $key
 * @return array|mixed
 */
function getDataByKey($data, $keyWord)
{
    if(empty($data)) return array();
    foreach($data as $key => $value)
    {
        if($value['id'] == $keyWord) return $value;
    }
    return array();
}

/**
 * response a json string to blowser
 * 
 * @param mixed $msg
 * @param intval $status true or false
 * @see Illuminate\Support\Facades\Response::json
 */
function responseJson($msg, $status = false)
{
    $status = $status ? 'success' : 'error';
    $arr = array('result' => $status, 'message' => $msg);
    return Response::json($arr);
}

/**
 * 生成URL
 * 
 * @param string $uri
 * @param array $param 附带的参数
 * @return string
 */
function U($uri, $param = array())
{
    $baseUrl = URL::to($uri);
    if( ! empty($param))
    {
        $paramString = '';
        $paramArray = array();
        foreach($param as $key => $value)
        {
            $paramArray[] = $key.'='.$value;
        }
        $paramString .= implode('&', $paramArray);
        $baseUrl .= '?' . $paramString;
    }
    return $baseUrl;
}

/**
 * 转换字节数为其他单位
 *
 * @param    string  $filesize   字节大小
 * @return   string  返回大小
 */
function sizecount($filesize)
{
    if($filesize >= 1073741824)
    {
        $filesize = round($filesize / 1073741824 * 100) / 100 .' GB';
    }
    elseif($filesize >= 1048576)
    {
        $filesize = round($filesize / 1048576 * 100) / 100 .' MB';
    }
    elseif($filesize >= 1024)
    {
        $filesize = round($filesize / 1024 * 100) / 100 . ' KB';
    }
    else
    {
        $filesize = $filesize.' Bytes';
    }
    return $filesize;
}

/**
* 转化 \ 为 /
* 
* @param	string	$path	路径
* @return	string	路径
*/
function dir_path($path)
{
    $path = str_replace('\\', '/', $path);
    if(substr($path, -1) != '/') $path = $path.'/';
    return $path;
}

/**
 * 创建目录
 * 
 * @param    string  $path   路径
 * @param    string  $mode   属性
 * @return   string  如果已经存在则返回true，否则为flase
 */
function dir_create($path, $mode = 0777)
{
    if(is_dir($path)) return TRUE;
    $ftp_enable = 0;
    $path = dir_path($path);
    $temp = explode('/', $path);
    $cur_dir = '';
    $max = count($temp) - 1;
    for($i=0; $i<$max; $i++)
    {
        $cur_dir .= $temp[$i].'/';
        if (@is_dir($cur_dir)) continue;
        @mkdir($cur_dir, 0777,true);
        @chmod($cur_dir, 0777);
    }
    return is_dir($path);
}

/**
 * 返回经addslashes处理过的字符串或数组
 * @param $string 需要处理的字符串或数组
 * @return mixed
 */
function new_addslashes($string)
{
    if(!is_array($string)) return addslashes($string);
    foreach($string as $key => $val) $string[$key] = new_addslashes($val);
    return $string;
}

/**
 * 安全过滤函数
 *
 * @param $string
 * @return string
 */
function safe_replace($string)
{
    $string = str_replace('%20','',$string);
    $string = str_replace('%27','',$string);
    $string = str_replace('%2527','',$string);
    $string = str_replace('*','',$string);
    $string = str_replace('"','&quot;',$string);
    $string = str_replace("'",'',$string);
    $string = str_replace('"','',$string);
    $string = str_replace(';','',$string);
    $string = str_replace('<','&lt;',$string);
    $string = str_replace('>','&gt;',$string);
    $string = str_replace("{",'',$string);
    $string = str_replace('}','',$string);
    $string = str_replace('\\','',$string);
    return $string;
}

/**
 * 取得文件扩展，主要用于文件上传插件
 *
 * @param $filename 文件名
 * @return 扩展名
 */
function fileext($filename)
{
    return strtolower(trim(substr(strrchr($filename, '.'), 1, 10)));
}

/**
 * 返回附件类型图标, 改函数主要用于文件上传插件中的目录浏览
 * 
 * @param $file 附件名称
 * @param $type png为大图标，gif为小图标
 */
function file_icon($file,$type = 'png')
{
    $ext_arr = array('doc','docx','ppt','xls','txt','pdf','mdb','jpg','gif','png','bmp','jpeg','rar','zip','swf','flv');
    $ext = fileext($file);
    if($type == 'png')
    {
        if($ext == 'zip' || $ext == 'rar') $ext = 'rar';
        elseif($ext == 'doc' || $ext == 'docx') $ext = 'doc';
        elseif($ext == 'xls' || $ext == 'xlsx') $ext = 'xls';
        elseif($ext == 'ppt' || $ext == 'pptx') $ext = 'ppt';
        elseif ($ext == 'flv' || $ext == 'swf' || $ext == 'rm' || $ext == 'rmvb') $ext = 'flv';
        else $ext = 'do';
    }
    if(in_array($ext,$ext_arr)) return STATIC_PATH.'/ext/v9/images/ext/'.$ext.'.'.$type;
    else return IMG_PATH.'/ext/v9/images/ext/blank.'.$type;
}